// 真机环境检测工具
export default class DeviceDetector {
  static isWeChatEnvironment() {
    return typeof wx !== 'undefined';
  }

  static isRealDevice() {
    // 检测是否在真机环境
    if (typeof wx !== 'undefined') {
      try {
        // 使用新的API替代废弃的getSystemInfoSync
        if (wx.getDeviceInfo) {
          const deviceInfo = wx.getDeviceInfo();
          return deviceInfo.platform === 'android' || deviceInfo.platform === 'ios';
        } else if (wx.getSystemInfoSync) {
          // 降级到旧API
          const systemInfo = wx.getSystemInfoSync();
          return systemInfo.platform === 'android' || systemInfo.platform === 'ios';
        } else {
          return false;
        }
      } catch (error) {
        console.warn('获取系统信息失败:', error);
        return false;
      }
    }
    return false;
  }

  static getDeviceInfo() {
    if (typeof wx !== 'undefined') {
      try {
        // 优先使用新API
        if (wx.getDeviceInfo) {
          return wx.getDeviceInfo();
        } else if (wx.getSystemInfoSync) {
          // 降级到旧API
          return wx.getSystemInfoSync();
        } else {
          return null;
        }
      } catch (error) {
        console.warn('获取设备信息失败:', error);
        return null;
      }
    }
    return null;
  }

  static isLowPerformanceDevice() {
    const deviceInfo = this.getDeviceInfo();
    if (deviceInfo) {
      // 检测低性能设备
      const memory = deviceInfo.memory || 0;
      const cpuCores = deviceInfo.cpuCores || 0;
      
      return memory < 2048 || cpuCores < 4; // 内存小于2GB或CPU核心数小于4
    }
    return false;
  }

  static shouldUseSimpleRendering() {
    // 在低性能设备上使用简单渲染
    return this.isRealDevice() && this.isLowPerformanceDevice();
  }

  static getOptimalCanvasSize() {
    const deviceInfo = this.getDeviceInfo();
    if (deviceInfo) {
      const { screenWidth, screenHeight } = deviceInfo;
      
      // 根据设备性能调整Canvas大小
      if (this.isLowPerformanceDevice()) {
        return {
          width: Math.min(screenWidth, 375),
          height: Math.min(screenHeight, 667)
        };
      }
      
      return {
        width: screenWidth,
        height: screenHeight
      };
    }
    
    // 默认值
    return {
      width: 375,
      height: 667
    };
  }

  static logDeviceInfo() {
    if (typeof wx !== 'undefined') {
      try {
        // 优先使用新API
        let systemInfo;
        if (wx.getDeviceInfo) {
          systemInfo = wx.getDeviceInfo();
        } else if (wx.getSystemInfoSync) {
          // 降级到旧API
          systemInfo = wx.getSystemInfoSync();
        } else {
          console.warn('无法获取设备信息');
          return;
        }
        
        console.log('设备信息:', {
          platform: systemInfo.platform,
          system: systemInfo.system,
          version: systemInfo.version,
          memory: systemInfo.memory,
          cpuCores: systemInfo.cpuCores,
          screenWidth: systemInfo.screenWidth,
          screenHeight: systemInfo.screenHeight
        });
      } catch (error) {
        console.warn('获取设备信息失败:', error);
      }
    }
  }
} 